Tutorial: Uso de la libreria de Google Finance en Python para leer datos de acciones

Paso 1: Importar las librerias necesarias


In [4]:
from googlefinance import getQuotes 
import time 
import json 
import os 
import sys 
from IPython.display import clear_output

Paso 2: Definir una funcion que imprime en formato JSON la informacion


In [5]:
def buscar_accion(nombre_accion):
    clear_output()
    os.system('cls' if os.name=='nt' else 'clear') 
    print(json.dumps(getQuotes(nombre_accion), indent=2))

Paso 3: Buscar informacion de la accion de Google (GOOG)


In [6]:
buscar_accion("AAPL")


[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
[
  {
    "ID": "22144",
    "StockSymbol": "AAPL",
    "Index": "NASDAQ",
    "LastTradePrice": "144.15",
    "LastTradeWithCurrency": "144.15",
    "LastTradeTime": "4:00PM EDT",
    "LastTradeDateTime": "2017-07-07T16:00:05Z",
    "LastTradeDateTimeLong": "Jul 7, 4:00PM EDT"
  }
]
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-6-0a2339af71ad> in <module>()
----> 1 buscar_accion("AAPL")

<ipython-input-5-e4ddd9edd382> in buscar_accion(nombre_accion)
      2     while True:
      3         os.system('cls' if os.name=='nt' else 'clear')
----> 4         print(json.dumps(getQuotes(nombre_accion), indent=2))
      5 

/Users/ricardosuarez/anaconda3/lib/python3.6/site-packages/googlefinance/__init__.py in getQuotes(symbols)
     68     if type(symbols) == type('str'):
     69         symbols = [symbols]
---> 70     content = json.loads(request(symbols))
     71     return replaceKeys(content);
     72 

/Users/ricardosuarez/anaconda3/lib/python3.6/site-packages/googlefinance/__init__.py in request(symbols)
     31     url = buildUrl(symbols)
     32     req = Request(url)
---> 33     resp = urlopen(req)
     34     # remove special symbols such as the pound symbol
     35     content = resp.read().decode('ascii', 'ignore').strip()

/Users/ricardosuarez/anaconda3/lib/python3.6/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221     else:
    222         opener = _opener
--> 223     return opener.open(url, data, timeout)
    224 
    225 def install_opener(opener):

/Users/ricardosuarez/anaconda3/lib/python3.6/urllib/request.py in open(self, fullurl, data, timeout)
    524             req = meth(req)
    525 
--> 526         response = self._open(req, data)
    527 
    528         # post-process response

/Users/ricardosuarez/anaconda3/lib/python3.6/urllib/request.py in _open(self, req, data)
    542         protocol = req.type
    543         result = self._call_chain(self.handle_open, protocol, protocol +
--> 544                                   '_open', req)
    545         if result:
    546             return result

/Users/ricardosuarez/anaconda3/lib/python3.6/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502         for handler in handlers:
    503             func = getattr(handler, meth_name)
--> 504             result = func(*args)
    505             if result is not None:
    506                 return result

/Users/ricardosuarez/anaconda3/lib/python3.6/urllib/request.py in http_open(self, req)
   1344 
   1345     def http_open(self, req):
-> 1346         return self.do_open(http.client.HTTPConnection, req)
   1347 
   1348     http_request = AbstractHTTPHandler.do_request_

/Users/ricardosuarez/anaconda3/lib/python3.6/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
   1319             except OSError as err: # timeout error
   1320                 raise URLError(err)
-> 1321             r = h.getresponse()
   1322         except:
   1323             h.close()

/Users/ricardosuarez/anaconda3/lib/python3.6/http/client.py in getresponse(self)
   1329         try:
   1330             try:
-> 1331                 response.begin()
   1332             except ConnectionError:
   1333                 self.close()

/Users/ricardosuarez/anaconda3/lib/python3.6/http/client.py in begin(self)
    295         # read until we get a non-100 response
    296         while True:
--> 297             version, status, reason = self._read_status()
    298             if status != CONTINUE:
    299                 break

/Users/ricardosuarez/anaconda3/lib/python3.6/http/client.py in _read_status(self)
    256 
    257     def _read_status(self):
--> 258         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    259         if len(line) > _MAXLINE:
    260             raise LineTooLong("status line")

/Users/ricardosuarez/anaconda3/lib/python3.6/socket.py in readinto(self, b)
    584         while True:
    585             try:
--> 586                 return self._sock.recv_into(b)
    587             except timeout:
    588                 self._timeout_occurred = True

KeyboardInterrupt: 

In [ ]: